package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.mapper.OmsCompanyAddressMapper;
import com.macro.mall.mapper.OmsOrderReturnApplyMapper;
import com.macro.mall.mapper.OmsOrderReturnReasonMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.domain.OmsOrderReturnApplyParam;
import com.macro.mall.portal.service.OmsPortalOrderReturnApplyService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.vo.OmsOrderReturnApplyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单退货管理Service实现类
 * Created by macro on 2020/7/31.
 */
@Service
public class OmsPortalOrderReturnApplyServiceImpl implements OmsPortalOrderReturnApplyService {
    @Autowired
    private OmsOrderReturnApplyMapper returnApplyMapper;
    @Autowired
    private OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;
    @Autowired
    private OmsCompanyAddressMapper omsCompanyAddressMapper;
    @Autowired
    private UmsMemberService memberService;
    @Override
    public int create(OmsOrderReturnApplyParam returnApply) {
        UmsMember member = memberService.getCurrentMember();
        OmsOrderReturnApply realApply = new OmsOrderReturnApply();
        BeanUtils.copyProperties(returnApply,realApply);
        realApply.setCreateTime(new Date());
        realApply.setStatus(0);
        realApply.setMemberUsername(member.getUsername());
        return returnApplyMapper.insert(realApply);
    }

    @Override
    public List<OmsOrderReturnApply> list(Integer status, Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        PageHelper.startPage(pageNum,pageSize);
        OmsOrderReturnApplyExample omsOrderReturnApplyExample = new OmsOrderReturnApplyExample();
        OmsOrderReturnApplyExample.Criteria criteria = omsOrderReturnApplyExample.createCriteria();
        criteria.andMemberUsernameEqualTo(member.getUsername());
        if (status!=-1) criteria.andStatusEqualTo(status);
        omsOrderReturnApplyExample.setOrderByClause("create_time desc");
        return returnApplyMapper.selectByExample(omsOrderReturnApplyExample);
    }

    @Override
    public List<OmsOrderReturnReason> returnReasonList() {
        OmsOrderReturnReasonExample omsOrderReturnReasonExample = new OmsOrderReturnReasonExample();
        omsOrderReturnReasonExample.createCriteria().andStatusEqualTo(1);
        return omsOrderReturnReasonMapper.selectByExample(omsOrderReturnReasonExample);
    }

    @Override
    public OmsOrderReturnApplyVO getById(Long id) {
        OmsOrderReturnApply omsOrderReturnApply = returnApplyMapper.selectByPrimaryKey(id);
        OmsOrderReturnApplyVO omsOrderReturnApplyVO = new OmsOrderReturnApplyVO();
        BeanUtils.copyProperties(omsOrderReturnApply,omsOrderReturnApplyVO);
        System.out.println(omsOrderReturnApply.getCompanyAddressId());
        if (omsOrderReturnApply.getCompanyAddressId()!=null) {
            OmsCompanyAddress omsCompanyAddress = omsCompanyAddressMapper.selectByPrimaryKey(omsOrderReturnApply.getCompanyAddressId());
            omsOrderReturnApplyVO.setOmsCompanyAddress(omsCompanyAddress);
        }
        System.out.println();
        return omsOrderReturnApplyVO;
    }
}
